﻿using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using Rodunu.Business.Models.Movies.Apple;
using Rodunu.Services.Interfaces;

namespace Rodunu.Services.AppleTrailers
{
    public class AppleTrailerService : IAppleTrailerService
    {
        public AppleTrailerService()
        {
        }

        public List<AppleTitle> GetTitles()
        {
            List<AppleTitle> appleTitles = new List<AppleTitle>();

            try
            {
                WebClient client = new WebClient();
                Stream strm = client.OpenRead(@"http://www.apple.com/trailers/home/feeds/just_hd.json");
                StreamReader sr = new StreamReader(strm);
                string json = sr.ReadToEnd();
                MemoryStream ms = new MemoryStream(System.Text.Encoding.Unicode.GetBytes(json));
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<AppleTitle>));
                appleTitles = ser.ReadObject(ms) as List<AppleTitle>;
                ms.Close();
            }
            catch { }

            return appleTitles;
        }
    }
}
